package com.forest.core.etx

import java.text.SimpleDateFormat
import java.util.*


fun formatDate(date: Long, format: SimpleDateFormat): String {
    return format.format(Date(if (date.toString().length < 13) date * 1000 else date))
}

fun formatDate(date: String, format: SimpleDateFormat): String {
    return format.format(date)
}

fun formatDate(date: Date, format: SimpleDateFormat): String {
    return format.format(date)
}

/**
 * 格式化当前日期
 */
fun formatCurrentDate(): String {
    return formatDate(Date(), SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE))
}

fun formatDiffTime(time: Long): String {
    val lastTime = if (time.toString().length < 13) time else time / 1000
    val diffTime = System.currentTimeMillis() / 1000 - lastTime
    val day = diffTime / (60 * 60 * 24)
    if (day > 0) {
        return "${day}天前"
    }
    val hour = (diffTime / (60 * 60)) % 24
    if (hour > 0) {
        return "${hour}小时前"
    }
    val minute = (diffTime / 60) % 60
    if (minute > 0) {
        return "${minute}分钟前"
    }
    return "刚刚"
}

/**
 * 将秒转成00:00
 */
fun formatSeconds2Hour(time: Int): String {
    var timeStr = ""
    var hour = 0
    var minute = 0
    var second = 0
    if (time <= 0)
        return "00:00"
    else {
        minute = time / 60
        if (minute < 60) {
            second = time % 60
            timeStr = unitFormat(minute) + ":" + unitFormat(second)
        } else {
            hour = minute / 60
            if (hour > 99)
                return "99:59:59"
            minute %= 60
            second = time - hour * 3600 - minute * 60
            timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second)
        }
    }
    return timeStr
}

fun unitFormat(i: Int): String {
    return if (i in 0..9) "0$i" else i.toString()
}